﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Model;
using NLog;
using NLog.Config;

namespace App
{
  class Program
  {
    static void Main(string[] args)
    {
      OneThreadSynchronizationContext context = new OneThreadSynchronizationContext();
      SynchronizationContext.SetSynchronizationContext(context);

      Game.EventSystem.Add(AssemblyType.Model, DllHelper.LoadModel());
      Game.EventSystem.Add(AssemblyType.Hotfix, DllHelper.LoadHotfix());

      Options options = Game.Scene.AddComponent<OptionComponent, string[]>(args).Options;
      StartConfig startConfig = Game.Scene.AddComponent<StartConfigComponent, string, int>(options.Config, options.AppId).StartConfig;

      LogManager.Configuration.Variables["appType"] = options.AppType.ToString();
      LogManager.Configuration.Variables["appId"] = options.AppId.ToString();

      Log.Info("server start ....");

      Game.Scene.AddComponent<OpcodeTypeComponent>();
      Game.Scene.AddComponent<MessageDispatcherComponent>();

      OuterConfig outerConfig = startConfig.GetComponent<OuterConfig>();
      InnerConfig innerConfig = startConfig.GetComponent<InnerConfig>();

      switch (options.AppType)
      {
        case AppType.Manager:
          break;
        case AppType.Gate:
          break;
        case AppType.Game:
          break;
        case AppType.DB:
          break;
        case AppType.Location:
          break;
        case AppType.All:
          Game.Scene.AddComponent<TestComponent>();
          Game.Scene.AddComponent<NetInnerComponent, IPEndPoint>(innerConfig.IPEndPoint);
          Game.Scene.AddComponent<NetOuterComponent, IPEndPoint>(outerConfig.IPEndPoint);
          break;
        default:
          break;
      }

      while (true)
      {
        Thread.Sleep(1);
        context.Update();
        Game.EventSystem.Update();
      }
    }
  }
}